<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/21
 * Time: 13:55
 */

namespace Admin\Controller;


use Think\Controller;

class FtthController extends AdminController
{
    //对父类构造函数的补充
    public function _myinitialize()
    {
        $this->db = M('ftth');
        $this->session_lst = CONTROLLER_NAME."lst";
    }


    public function lst()
    {
        $session_where=I('session_where');//不为空，则表明是从add/edit/del中跳转过来的，查询条件从session中取。
        $where=array();
        if(empty($session_where)){

            $dq_arr=$this->db->where($where)->distinct(true)->getField('dq',true);//所有开通地区列表
            $ktdd_arr = $this->db->where($where)->distinct(true)->getField('ktdd', true);//所有开通地点列表
            $this->assign('dq_arr',$dq_arr);
            $this->assign('ktdd_arr',$ktdd_arr);

            $month = I('month');
            $year = I('year');
            if (!empty($month) && !empty($year)) { //所属月份
                $where['yf'] = $year . $month;
            }

            if (I('dq')) { //地区
                $where['dq'] = I('dq');
            }

            if (I('ktdd')) {  //开通地点
                $where['ktdd'] = I('ktdd');
            }

            if(I('ywzh')){ //业务账号
                $where['ywzh']=I('ywzh');
            }

            if(I('ktrq')){ // 开通日期
                $where['ktrq']=I('ktrq');
            }

            if(I("jsrq")){ //结束日期
                $where['jsrq']=I('jsrq');
            }

            if(count($where)===0){
                //默认查询条件
                $where['dq']="成都";
                $where['yf']=date("Ym");
            }

            //将查询条件存入session中，方便更新时跳转
            session(CONTROLLER_NAME."where",$where);
        }else{
            $where=session(CONTROLLER_NAME."where");
        }

        //列表数据
        $lsts = $this->db->where($where)->order("updatetime desc,jsrq desc")->select();

        $this->assign('lsts', $lsts);//列表数据

        //将数据存入session中，方便导出
        session($this->session_lst, null);
        session($this->session_lst, $lsts);

        $this->display();
    }

    //新增 www.glxt.com/admin/khzy/add
    public function add()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['yf'] = date('Ym', strtotime($data['sssj']));
                $data['xj'] = $data['yjwj'] + $data['jfje']+$data['azfy'];//本月实际费用小计=应缴未缴金额+缴费金额+安装费用
                $data['updatetime']=time();
            }
            $rid = $this->db->add($data);

            if ($rid === false) {
                $this->error($this->db->getDbError());
            }

            $this->redirect('lst',array("session_where"=>"session_where"));
        }
    }

    //修改
    public function edit()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['yf'] = date('Ym', strtotime($data['sssj']));
                $data['xj'] = $data['yjwj'] + $data['jfje']+$data['azfy'];//本月实际费用小计=应缴未缴金额+缴费金额+安装费用
                $data['updatetime']=time();
            }
            $rid = $this->db->save($data);

            if ($rid === false) {
                $this->error($this->db->getDbError());
            }

            $this->redirect('lst',array("session_where"=>"session_where"));
        } else { //回显
            $id = I('id');
            $data = $this->db->where(array('id' => $id))->find();
            $this->ajaxReturn($data);
        }
    }

    //删除不是物理删除，只是状态的改变
    public function del()
    {
        $id = I('id');
        if (!empty($id)) {
            $this->db->where(array('id' => $id))->delete();
            $this->redirect('lst',array("session_where"=>"session_where"));
        }
    }

    //通过excel导入数据
    public function import()
    {
        if (IS_POST) {
            $this->import_excel();
        }
    }

    //根据表的不同来处理数据
    protected function handle_data($data)
    {

        foreach ($data as $k => $v) {
            $data[$k]['yf'] = date('Ym', strtotime($data[$k]['sssj']));
            $data[$k]['yxj'] = $data[$k]['yjwj'] + $data[$k]['jfje']+$data[$k]['azfy'];//本月实际费用小计=应缴未缴金额+缴费金额+安装费用
            $data[$k]['updatetime']=time();
        }

        return $data;
    }

    //导出数据到csv
    public function csv_export(){
        $this->export();
    }

    //导出数据到excel，用phpexcel无论怎么样都无法成功，弃用
    public function excel_export()
    {
        $data = session($this->session_lst);//从session中取出需要导出的数据
        $name = "Ftth" . date('YmdHis');//导出文件名

        $range=range("A","Z");
        foreach($range as $v){
            array_push($range,"A".$v);//$range为A-AZ共52列，本项目中表字段的个数不会超过52
        }
        Vendor('Excel.PHPExcel');
        $objPHPExcel = new \PHPExcel();

        //表头
        $a0=reset($data);//$a0表示第一个元素
        $head=array_keys($a0);//表头的数组

        foreach($head as $k=>$v){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($range[$k]."1",$v);
        }

        //往数据表中填入数据，先横向一个单元格一个单元格的填
        $num=0;
        foreach($data as $k=>$v){
            $num+=1; //$num表示第几行
            $i=0;
            foreach($v as $v1){ //$v的元素个数就是列的总数
                $i+=1;//$i表示第几列
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($range[$i].$num,$v1);
            }
        }

        $objPHPExcel->getActiveSheet()->setTitle($name);
        $objPHPExcel->setActiveSheetIndex(0);
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename='.$name.'.xlsx');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
        ob_end_clean();
        $objWriter->save('php://output');
        exit;
    }


}